package dao

import (
	"context"
	"fmt"
	"github.com/zeromicro/go-zero/core/stores/sqlc"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
	"go-admin/base/utils"
	"strings"
)

var _ RolePermissionModel = (*customRolePermissionModel)(nil)

type (
	// RolePermissionModel is an interface to be customized, add more methods here,
	// and implement the added methods in customRolePermissionModel.
	RolePermissionModel interface {
		rolePermissionModel
		FindRolePermissionsByPermissionId(ctx context.Context, permissionId int64) ([]*RolePermission, error)
		DeleteByIdsSession(ctx context.Context, session sqlx.Session, ids []int64) error
		FindRolePermissionsByPermissionIds(ctx context.Context, permissionIds []int64) ([]*RolePermission, error)
		FindRolePermissionsByRoleId(ctx context.Context, roleId int64) ([]*RolePermission, error)
		FindRolePermissionsByRoleIds(ctx context.Context, roleIds []int64) ([]*RolePermission, error)
		BatchInsert(ctx context.Context, conn sqlx.SqlConn, rolePermissions []*RolePermission) error
	}

	customRolePermissionModel struct {
		*defaultRolePermissionModel
	}
)

func (c customRolePermissionModel) BatchInsert(ctx context.Context, conn sqlx.SqlConn, rolePermissions []*RolePermission) error {
	query := fmt.Sprintf("insert into %s (%s) values (?, ?)", c.table, userRoleRowsExpectAutoSet)
	bulkInserter, _ := sqlx.NewBulkInserter(conn, query)
	defer bulkInserter.Flush()
	for _, rolePermission := range rolePermissions {
		err := bulkInserter.Insert(rolePermission.RoleId, rolePermission.PermissionId)
		if err != nil {
			return err
		}
	}
	return nil
}

func (c customRolePermissionModel) DeleteByIdsSession(ctx context.Context, session sqlx.Session, ids []int64) error {
	query := fmt.Sprintf("delete from %s where `id` in ( %s )", c.table, strings.Join(utils.GeneratePlaceholderByInt64(ids), ","))
	_, err := session.ExecCtx(ctx, query, utils.ConvertToAny(ids)...)
	return err
}

func (c customRolePermissionModel) FindRolePermissionsByPermissionId(ctx context.Context, permissionId int64) ([]*RolePermission, error) {
	query := fmt.Sprintf("select %s from %s where `permission_id` = ? ", rolePermissionRows, c.table)
	var resp []*RolePermission
	err := c.conn.QueryRowCtx(ctx, &resp, query, permissionId)
	switch err {
	case nil:
		return resp, nil
	case sqlc.ErrNotFound:
		return nil, ErrNotFound
	default:
		return nil, err
	}
}

func (c customRolePermissionModel) FindRolePermissionsByPermissionIds(ctx context.Context, permissionIds []int64) ([]*RolePermission, error) {
	query := fmt.Sprintf("select %s from %s where `permission_id` in ( %s ) ", rolePermissionRows, c.table, strings.Join(utils.GeneratePlaceholderByInt64(permissionIds), ","))
	var resp []*RolePermission
	err := c.conn.QueryRowsCtx(ctx, &resp, query, utils.ConvertToAny(permissionIds)...)
	switch err {
	case nil:
		return resp, nil
	case sqlc.ErrNotFound:
		return nil, ErrNotFound
	default:
		return nil, err
	}
}

func (c customRolePermissionModel) FindRolePermissionsByRoleId(ctx context.Context, roleId int64) ([]*RolePermission, error) {
	query := fmt.Sprintf("select %s from %s where `role_id` = ? ", rolePermissionRows, c.table)
	var resp []*RolePermission
	err := c.conn.QueryRowCtx(ctx, &resp, query, roleId)
	switch err {
	case nil:
		return resp, nil
	case sqlc.ErrNotFound:
		return nil, ErrNotFound
	default:
		return nil, err
	}
}

func (c customRolePermissionModel) FindRolePermissionsByRoleIds(ctx context.Context, roleIds []int64) ([]*RolePermission, error) {
	query := fmt.Sprintf("select %s from %s where `role_id` in ( %s ) ", rolePermissionRows, c.table, strings.Join(utils.GeneratePlaceholderByInt64(roleIds), ","))
	var resp []*RolePermission
	err := c.conn.QueryRowsCtx(ctx, &resp, query, utils.ConvertToAny(roleIds)...)
	switch err {
	case nil:
		return resp, nil
	case sqlc.ErrNotFound:
		return nil, ErrNotFound
	default:
		return nil, err
	}
}

// NewRolePermissionModel returns a model for the database table.
func NewRolePermissionModel(conn sqlx.SqlConn) RolePermissionModel {
	return &customRolePermissionModel{
		defaultRolePermissionModel: newRolePermissionModel(conn),
	}
}
